//
//  QSWorkStationDataViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/5/18.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSWorkStationDataViewController.h"
#import "QSWorkStationProgressCell.h"
#import "QSWorkStationStateCell.h"
#import "QSWorkStationDetailsViewController.h"
#import "QSWorkStationMyWorkViewController.h"
#import "QSWorkStationInterface.h"
#import "QSWorkStationTimeModel.h"
#import "QSWorkStationAllListModel.h"
#import "QSTodayNullCell.h"
@interface QSWorkStationDataViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, strong)NSArray *workArray;
@property (nonatomic, assign)BOOL canLook;
@property (nonatomic, assign)BOOL Today;//如果没有今天的数据 则强行加入一条空数据
@end
static NSString *workStationProgressCell = @"WorkStationProgressCell";
static NSString *workStationStateCell = @"WorkStationStateCell";
static NSString *todayCell = @"TodayNullCell";

@implementation QSWorkStationDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configSubView];
    [self addRefreshHeader];
//    [self addRefreshFooter];
    [self startRefreshing];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [self startRefreshing];
}



- (void)configSubView{
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavgationBarHeight - kRealValue(44) - kTabBarHeight);
    
    NSLog(@"0000= %.2f", self.tableView.width);
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSWorkStationProgressCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:workStationProgressCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSWorkStationStateCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:workStationStateCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSTodayNullCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:todayCell];
}

#pragma mark - **************** UITableView Delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    QSWorkStationTimeModel *timeModel = self.dataArray[section];
    if ([timeModel.createTime isEqualToString:@"今日"]) {
        return 1;
    }else{
        NSMutableArray *workArray = [NSMutableArray array];
        [workArray addObjectsFromArray:timeModel.list];
        return workArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QSWorkStationTimeModel *timeModel = self.dataArray[indexPath.section];
    if ([timeModel.createTime isEqualToString:@"今日"]) {
        QSTodayNullCell *cell = [tableView dequeueReusableCellWithIdentifier:todayCell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }else{
        if ([self.titleStr isEqualToString:@"全部"]) {
            QSWorkStationProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:workStationProgressCell forIndexPath:indexPath];
            NSLog(@"66666 = %.2f", cell.progressView.width);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.workModel = timeModel.list[indexPath.row];
            return cell;
        }else{
            QSWorkStationStateCell *cell = [tableView dequeueReusableCellWithIdentifier:workStationStateCell forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.workModel = timeModel.list[indexPath.row];
            return cell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRealValue(110 + 10);//cell间距
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kRealValue(37);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    QSWorkStationTimeModel *timeModel = self.dataArray[section];
    UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 37)];
    if ([timeModel.createTime isEqualToString:@"今日"]) {
        dateLabel.text = timeModel.createTime;
    }else{
        NSString *dateStr = [NSDate dateWithTimestamp:timeModel.createTime format:@"yyyy年MM月dd日"];
        NSString *todayStr = [NSDate getCurrentTimesWithFormat:@"yyyy年MM月dd日"];
        if ([todayStr isEqualToString:dateStr]) {
            dateLabel.text = @"今日";
        }else{
            dateLabel.text = dateStr;
        }
    }
    return dateLabel;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    QSWorkStationTimeModel *timeModel = self.dataArray[indexPath.section];
    if ([timeModel.createTime isEqualToString:@"今日"]) {
        [QSAppKeyWindow showAutoHideHudWithText:@"今日没有数据哦~"];
    }else{
        QSWorkStationAllListModel *workModel = timeModel.list[indexPath.row];
        if ([self.titleStr isEqualToString:@"全部"]) {
            QSWorkStationDetailsViewController *vc = [QSWorkStationDetailsViewController new];
            vc.workId = workModel.ewId;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            QSWorkStationMyWorkViewController *vc = [QSWorkStationMyWorkViewController new];
            vc.workId = workModel.ewId;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

//去掉tableview的粘性 使header联动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 39;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

-(void)tableViewShouldUpdateDataByPageIndex:(NSInteger)pageIndex{
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];

    if ([self.titleStr isEqualToString:@"全部"]) {
        //全部工作
        [QSWorkStationInterface getAllWorkListWithPageIndex:pageIndex andBlock:^(ResponseMessage *rspStatusAndMessage, QSWorkStationDataModel *resultModel) {
            [QSAppKeyWindow hideHud];
            if (rspStatusAndMessage.code == kResponseSuccessCode) {
                self.canLook = resultModel.isJingXin;//权限
                if (resultModel.isJingXin) {//权限
                    if (pageIndex == 1) {
                        [self.dataArray removeAllObjects];
                    }
                    
                    QSWorkStationTimeModel *timeModel = resultModel.list.firstObject;
                    NSString *dateStr = [NSDate dateWithTimestamp:timeModel.createTime format:@"yyyy年MM月dd日"];
                    NSString *todayStr = [NSDate getCurrentTimesWithFormat:@"yyyy年MM月dd日"];
                    
                    if ([todayStr isEqualToString:dateStr] || resultModel.list.count == 0) {
                    }else{
                        QSWorkStationTimeModel *timeModel = [[QSWorkStationTimeModel alloc]init];
                        timeModel.createTime = @"今日";
                        self.Today = YES;
                        [self.dataArray addObject:timeModel];
                    }
                    [self.dataArray addObjectsFromArray:resultModel.list];
                    self.tableView.emptyDataSetSource = self;
                    self.tableView.emptyDataSetDelegate = self;
                    [self.tableView reloadData];
                    [self.tableView qs_endRefreshing];
                }else{
                    //无权限
                    [self.dataArray removeAllObjects];
                    self.tableView.emptyDataSetSource = self;
                    self.tableView.emptyDataSetDelegate = self;
                    [self.tableView reloadData];
                    [self.tableView qs_endRefreshing];
                }
//                if (pageIndex >= resultModel.pages) {
//                    //                self.tableView.mj_footer.hidden = YES;
//                    [self.tableView qs_endRefreshingWithNoMoreData];
//                }
            }
        }];
    }else{
        //我的工作
        [QSWorkStationInterface getMyWorkListWithPageIndex:pageIndex andBlock:^(ResponseMessage *rspStatusAndMessage, QSWorkStationDataModel *resultModel) {
            [QSAppKeyWindow hideHud];
            if (rspStatusAndMessage.code == kResponseSuccessCode) {
                self.canLook = resultModel.isEnterprisePerson;//权限
                if (resultModel.isEnterprisePerson) {
                    if (pageIndex == 1) {
                        [self.dataArray removeAllObjects];
                    }
                    QSWorkStationTimeModel *timeModel = resultModel.list.firstObject;
                    NSString *dateStr = [NSDate dateWithTimestamp:timeModel.createTime format:@"yyyy年MM月dd日"];
                    NSString *todayStr = [NSDate getCurrentTimesWithFormat:@"yyyy年MM月dd日"];
                    
                    if ([todayStr isEqualToString:dateStr] || resultModel.list.count == 0) {
                        //有今日数据
                    }else{
                        //没有手动添加
                        QSWorkStationTimeModel *timeModel = [[QSWorkStationTimeModel alloc]init];
                        timeModel.createTime = @"今日";
                        self.Today = YES;
                        [self.dataArray addObject:timeModel];
                    }
                    [self.dataArray addObjectsFromArray:resultModel.list];
                    self.tableView.emptyDataSetSource = self;
                    self.tableView.emptyDataSetDelegate = self;
                    [self.tableView reloadData];
                    [self.tableView qs_endRefreshing];
                }else{
                    //无权限
                    [self.dataArray removeAllObjects];
                    self.tableView.emptyDataSetSource = self;
                    self.tableView.emptyDataSetDelegate = self;
                    [self.tableView reloadData];
                    [self.tableView qs_endRefreshing];
                }
//                if (pageIndex >= resultModel.pages) {
//                    //                self.tableView.mj_footer.hidden = YES;
//                    [self.tableView qs_endRefreshingWithNoMoreData];
//                }
            }
        }];
    }
    
}


#pragma mark - DZNEmptyDataSetSource
/**
 *  返回标题文字
 */
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text;
    if (self.canLook) {
        text = @"暂无工作!";
    }else{
        text = @"抱歉，当前只有驻企服务员 需要处理工作!";
    }
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f],
                                     NSForegroundColorAttributeName: [UIColor QSColorGray999999] };
    return [[NSAttributedString alloc]initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    scrollView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    if (self.canLook) {
        return [UIImage imageNamed:@"ic_work_null"];
    }else{
         return [UIImage imageNamed:@"ic_notpermissions"];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
